/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.entity.ZtQuan;
import com.jeesite.modules.goods.service.ZtQuanService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;

/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequiresPermissions(value="quan:yhQuan:view")
@RequestMapping(value = "${adminPath}/ztQuan")
public class ZtQuanController extends BaseController {

	@Autowired
	private ZtQuanService ztQuanService;
	
	@Autowired	
	private UserService userService;
	
	@Autowired  
    private MyProps myProps;

	
	

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtQuan get(String id, boolean isNewRecord) {
		ZtQuan ztQuan=new ZtQuan();
		ztQuan.setId(id);
		return ztQuanService.get(ztQuan);
	}

	/**
	 * 查询列表
	 */
	
	@RequiresPermissions(value="quan:yhQuan:view")
	@RequestMapping(value = { "list", "" })
	public String list(ZtQuan ztQuan, Model model) {
		model.addAttribute("ztQuan", ztQuan);
		
		
		return "modules/ztQuan/ztQuanList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions(value="quan:yhQuan:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtQuan> listData(ZtQuan ztQuan, HttpServletRequest request,
			HttpServletResponse response,Model model) {
		
		Page<ZtQuan> page = ztQuanService.findPage(new Page<ZtQuan>(request,
				response), ztQuan);
		
		
		
		return page;
	}
	
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions(value="quan:yhQuan:view")
	@RequestMapping(value = "form")
	public String form(@ModelAttribute("ztQuan") ZtQuan ztQuan, Model model,HttpServletRequest request,HttpServletResponse response) {
		
		if(ztQuan.getCreateDate()==null){
			ztQuan.setCreateDate(new Date());
		}
		
		model.addAttribute("ztQuan", ztQuan);
		

		User user=new User();
		user.setMgrType(User.MGR_TYPE_CORP_ADMIN);	// 租户管理员
		// 禁用自动添加租户代码条件，添加自定义租户查询条件
		user.getSqlMap().getWhere().disableAutoAddCorpCodeWhere()
			.and("corp_code", QueryType.EQ, user.getCorpCode_())
			.and("corp_name", QueryType.LIKE, user.getCorpName_());
		
		List<User> userList = userService.findList(user);
			//	userList
		model.addAttribute("userList", userList);
		
		return "modules/ztQuan/ztQuanForm";
	}
	
	/**
	 * 保存数据
	 */
	@RequiresPermissions(value="quan:yhQuan:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZtQuan ztQuan) {
		
		if(ztQuan.getOverCount()==null){
			ztQuan.setOverCount(ztQuan.getTotal());
		}
		ztQuanService.save(ztQuan);
		
		return renderResult(Global.TRUE, text("保存卡券成功！"));
	}
	
	/**
	 * 失效
	 */
	@RequiresPermissions(value="quan:yhQuan:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(ZtQuan ztQuan) {
		ztQuan.setUsestatus("1");
		ztQuanService.updateUsestatus(ztQuan);
		return renderResult(Global.TRUE, text("券变更为失效成功"));
	}
	
	/**
	 * 
	 */
	@RequiresPermissions(value="quan:yhQuan:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(ZtQuan ztQuan) {
		ztQuan.setUsestatus("0");
		ztQuanService.updateUsestatus(ztQuan);
		return renderResult(Global.TRUE, text("券变更为有效成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions(value="quan:yhQuan:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZtQuan ztQuan) {
		ztQuanService.delete(ztQuan);
		return renderResult(Global.TRUE, text("删除卡券成功！"));
	}
}